package com.likou.second_challenge;

public class q98 {

    public boolean isValidBST(TreeNode root) {
        return check(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }

    public boolean check(TreeNode root, long min, long max) {
        if (root == null) {
            return true;
        }

        if (root.val >= max || root.val <= min) {
            return false;
        }

        return check(root.left, min, (long) root.val) && check(root.right, (long) root.val, max);
    }
}
